package com.jiyun.controller;


import com.jiyun.model.dto.ServiceIds;
import com.jiyun.model.information.MiddleTableItempmService;
import com.jiyun.model.information.ZlItempm;
import com.jiyun.model.information.ZlLabel;
import com.jiyun.model.information.ZlService;
import com.jiyun.result.Result;
import com.jiyun.service.IMiddleTableItempmServiceService;
import com.jiyun.service.IZlServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-24
 */
@RestController
@RequestMapping("/service/")
@Api("服务项目管理")
@Slf4j
public class ZlServiceController {


    @Autowired
    private IZlServiceService serviceService;

    @Autowired
    private IMiddleTableItempmServiceService middleTableItempmServiceService;

    @RequestMapping("findAll")
    public Result findAll(@RequestBody ZlService zlService) {
        System.err.println(zlService);

        if (!(zlService.getGrade() == null || zlService.getGrade().trim().isEmpty())
                && !(zlService.getName() == null || zlService.getName().trim().isEmpty())) {
            List<ZlService> list = serviceService.listAll();
            return Result.ok(list);
        } else {
            List<ZlService> list = serviceService.listByZlService(zlService);
//            System.err.println("服务包查询"+list);
            return Result.ok(list);
        }
//        return Result.ok();
    }


    @ApiModelProperty("查询包下面的服务")
    @RequestMapping("findItempm/{id}")
    public Result findItempm(@PathVariable Integer id) {
        System.err.println(id);
        List<ZlItempm> itempm = serviceService.findItempm(id);
        log.info("查询到的服务：" + itempm);
        return Result.ok(itempm);
    }


    @ApiModelProperty("状态变更")
    @RequestMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Integer id, @PathVariable String status) {
        ZlService byId = serviceService.getById(id);
        byId.setStatus(status);
        serviceService.updateById(byId);
        return Result.ok();
    }

//
    @ApiModelProperty("查询某个团队的服务包")
    @RequestMapping("findTeamService/{id}")
    public Result findTeamService(@PathVariable Integer id) {
        List<ZlService> zlServices =    serviceService.findTeamService(id);
        return Result.ok(zlServices);
    }


    @ApiModelProperty("查询服务包标签")
    @RequestMapping("findLabe")
    public List<ZlLabel> findLabe() {
        return serviceService.findLabel();
    }


    @ApiModelProperty("添加或修改服务包信息")
    @RequestMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ServiceIds serviceIds) {
//        log.info("添加或修改服务项目管理信息：" + serviceIds.getZlService());
//        log.info("ids= " + serviceIds.getIds());

        ZlService zlService = serviceIds.getZlService();

        try {
            if (zlService.getId() == null) {
                zlService.setStatus("2");
                serviceService.save(zlService);

                List<Integer> ids = serviceIds.getIds();
                List<MiddleTableItempmService> middleTableItempmServiceList = new ArrayList<>();
                for (Integer id : ids) {
                    MiddleTableItempmService item = new MiddleTableItempmService();
                    item.setIid(id);
                    item.setSid(zlService.getId());
                    middleTableItempmServiceList.add(item);
                }
//                List<MiddleTableItempmService> middleTableItempmServiceList = ids.stream()
//                        .map(id -> {
//                            MiddleTableItempmService item = new MiddleTableItempmService();
//                            item.setIid(id);
//                            item.setSid(zlService.getId());
//                            return item;
//                        })
//                        .collect(Collectors.toList());

//                log.info("要添加的集合：" + middleTableItempmServiceList);
                middleTableItempmServiceService.saveBatch(middleTableItempmServiceList);
            } else {
                serviceService.updateById(zlService);
            }
        } catch (Exception e) {
//            log.error("保存或更新服务项目时发生错误", e);
            return Result.fail("保存或更新失败");
        }

        return Result.ok();
    }

    @ApiModelProperty("查询所有")
    @RequestMapping("findAllServiceList")
    public Result findService() {
        List<ZlService> list = serviceService.list();
        return Result.ok(list);
    }
    @ApiModelProperty("根据ID查询")
    @RequestMapping("get/{id}")
    public ZlService getById(@PathVariable Integer id) {
        ZlService zlService = serviceService.getById(id);
        return zlService;
    }
}
